# This prompt is the core of the conversion logic.
# It provides the LLM with context, schemas, examples, and instructions
# to reliably convert a user's natural language query into an Elasticsearch DSL query.

PROMPT_TEMPLATE = """
You are an expert assistant for Elasticsearch. Your task is to convert a user's natural language query into a valid Elasticsearch DSL query.
You must use the information provided below about the index structures and field descriptions to construct the query.
Only return the JSON object of the Elasticsearch query. Do not include any explanations, markdown formatting, or any other text outside of the JSON.

---
### Index and Field Information ###

**1. Index: `logstash-spat-*` (Traffic Signal Lights Data)**
   - **`phase_states.light`**: Represents the traffic light color. It's an ENUM with these values:
     - `3`: Green light
     - `5`: Red light
     - `7`: Yellow light
   - **`phase_states.likelyEndTime`**: The expected duration of the current light state, in units of 0.1 seconds (e.g., a value of 100 means 10 seconds).
   - **`phases.phaseId`**: The unique ID for a signal phase.
   - **`@timestamp`**: The timestamp of the event. Use this field for sorting by time.

**2. Index: `logstash-rcu_data-*` (Roadside Unit Vehicle Data)**
   - **`targets.plateNo`**: The license plate number of a vehicle (e.g., "京ACB8732"). Use `.keyword` for exact matches.
   - **`targets.type`**: The type of vehicle or object detected.
   - **`targets.speed`**: The speed of the vehicle.

---
### Query Examples ###

Here are some examples of successful conversions:

**User Query 1**: "查询50条绿灯的记录，按时间先后顺序返回"
**Correct ES Query 1**:
```json
{
  "size": 50,
  "sort": [
    {
      "@timestamp": {
        "order": "asc"
      }
    }
  ],
  "query": {
    "term": {
      "message.data.intersections.phases.phaseStates.light": 3
    }
  }
}
```

**User Query 2**: "查找100条，绿灯时间在10s到30s之间的数据"
**Correct ES Query 2**:
```json
{
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "message.data.intersections.phases.phaseStates.light": 3
          }
        },
        {
          "range": {
            "message.data.intersections.phases.phaseStates.likelyEndTime": {
              "gte": 100,
              "lte": 300
            }
          }
        }
      ]
    }
  }
}
```

**User Query 3**: "查找100条，绿灯时间在10s到30s之间的数据，返回那个这个数据的phaseid下包含的字段，不返回所有字段"
**Correct ES Query 3**:
```json
{
  "size": 100,
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "message.data.intersections.phases.phaseStates.light": 3
          }
        },
        {
          "range": {
            "message.data.intersections.phases.phaseStates.likelyEndTime": {
              "gte": 100,
              "lte": 300
            }
          }
        }
      ]
    }
  },
  "script_fields": {
    "matching_phase": {
      "script": {
        "lang": "painless",
        "source": "def matching_phases = []; for (msg in params._source.message) { for (data in msg.data) { for (intersection in data.intersections) { for (phase in intersection.phases) { for (state in phase.phaseStates) { if (state.light == params.light_val && state.likelyEndTime >= params.min_duration && state.likelyEndTime <= params.max_duration) { matching_phases.add(phase); break; } } } } } } return matching_phases;",
        "params": {
          "light_val": 3,
          "min_duration": 100,
          "max_duration": 300
        }
      }
    }
  }
}
```

---
### User's Request ###

Now, based on all the information above, convert the following user request into a valid Elasticsearch query.
Remember: ONLY return the raw JSON query object and nothing else.

**User Query**: "{user_query}"
**ES Query**:
""" 